package com.nondo.dean.platform.controller.wap.app;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.aliyuncs.vod.model.v20170321.RefreshUploadVideoResponse;
import com.nondo.dean.common.domain.Video;
import com.nondo.dean.common.tool.AliyunVideoTool;
import com.nondo.dean.common.web.View;

@Controller("apiWapVideo")
@RequestMapping("/wap/app/video")
public class VideoController {
    @Autowired
    AliyunVideoTool aliyunVideoTool;

    /**
     * 获取上传地址和凭证
     *
     * @param request
     * @param response
     * @param video
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/upload/auth")
    public View<CreateUploadVideoResponse> uploadAuth(HttpServletRequest request,
                                                HttpServletResponse response, @RequestBody Video video) {
        View<CreateUploadVideoResponse> view = new View<CreateUploadVideoResponse>(
                20000, "授权成功");
        try {

            CreateUploadVideoResponse videoResponse = aliyunVideoTool
                    .createUploadVideo(video);
            view.setData(videoResponse);
        } catch (ClientException e) {
            e.printStackTrace();
            return View.ofError("获取上传地址和凭证失败"+e.getErrMsg());
        }
        return view;
    }

    /**
     * 刷新视频上传凭证
     *
     * @param request
     * @param response
     * @param video
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/refresh/auth")
    public View<RefreshUploadVideoResponse> refreshAuth(
            HttpServletRequest request, HttpServletResponse response,
            @RequestBody Video video) {
        View<RefreshUploadVideoResponse> view = new View<RefreshUploadVideoResponse>(
                20000, "授权成功");
        try {
            RefreshUploadVideoResponse videoResponse = aliyunVideoTool
                    .refreshUploadVideo(video.getVideoId());
            view.setData(videoResponse);
        } catch (ClientException e) {
            e.printStackTrace();
            return View.ofError("刷新视频上传凭证失败"+e.getErrMsg());
        }
        return view;
    }
    
    /**
     * 获取播放凭证
     *
     * @param request
     * @param response
     * @param video
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/play/auth")
    public View<GetVideoPlayAuthResponse> playAuth(HttpServletRequest request,
            HttpServletResponse response, @RequestBody Video video, BindingResult result) {
        View<GetVideoPlayAuthResponse> view = new View<GetVideoPlayAuthResponse>(
            20000, "授权成功");
        try {
            GetVideoPlayAuthResponse authResponse = aliyunVideoTool
                    .getVideoPlayAuth(video.getVideoId());
            view.setData(authResponse);
        } catch (ClientException e) {
            return View.ofError("获取播放凭证失败:"+e.getErrMsg());
        }
        return view;
    }
    
}